//
//  EPButton.m
//  EPTool
//
//  Created by guoshiqing on 2018/4/13.
//  Copyright © 2018年 云数动能科技. All rights reserved.
//

#import "EPButton.h"
#import "EPColor+EPButton.h"
#import "NSString+FixFormatError.h"
#import <ReactiveObjC/ReactiveObjC.h>
#import "Macro.h"

@interface EPButton ()
@property (nonatomic, strong) NSMutableDictionary *borderColorMap;
@property (nonatomic, strong) NSMutableDictionary *backgroundColorMap;
@end

@implementation EPButton

/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self addObserver];
    }
    return self;
}

- (void)awakeFromNib
{
    [super awakeFromNib];
    [self addObserver];
}

- (void)setEpStyle:(EPBasicButtonStyle)epStyle
{
    _epStyle = epStyle;
    switch (epStyle) {
        case EPBasicButton_btn1:
        {
            self.layer.cornerRadius = 4.f;
            self.layer.masksToBounds = YES;
            [self setTitleColor:[EPColor epColor_btn1_text_disable] forState:UIControlStateDisabled];
            [self setTitleColor:[EPColor epColor_btn1_text_normal]  forState:UIControlStateNormal];
            [self setTitleColor:[EPColor epColor_btn1_text_press]  forState:UIControlStateHighlighted];
            
            [self setBackgroundColor:[EPColor epColor_btn1_fill_disable] forState:UIControlStateDisabled];
            [self setBackgroundColor:[EPColor epColor_btn1_fill_normal] forState:UIControlStateNormal];
            [self setBackgroundColor:[EPColor epColor_btn1_fill_press] forState:UIControlStateHighlighted];
        }
            break;
        case EPBasicButton_btn2:
        {
            self.layer.cornerRadius = 4.f;
            self.layer.masksToBounds = YES;
            [self setTitleColor:[EPColor epColor_btn2_text_disable] forState:UIControlStateDisabled];
            [self setTitleColor:[EPColor epColor_btn2_text_normal]  forState:UIControlStateNormal];
            [self setTitleColor:[EPColor epColor_btn2_text_press]  forState:UIControlStateHighlighted];
            
            [self setBackgroundColor:[EPColor epColor_btn2_fill_disable] forState:UIControlStateDisabled];
            [self setBackgroundColor:[EPColor epColor_btn2_fill_normal] forState:UIControlStateNormal];
            [self setBackgroundColor:[EPColor epColor_btn2_fill_press] forState:UIControlStateHighlighted];
        }
            break;
        case EPBasicButton_btn3:
        {
            self.layer.cornerRadius = MIN(self.frame.size.width, self.frame.size.height)/2;
            self.layer.masksToBounds = YES;
            
            [self setTitleColor:[EPColor epColor_descMessageGray]  forState:UIControlStateNormal];
            [self setTitleColor:[EPColor epColor_Nav]  forState:UIControlStateHighlighted];
            
            [self setBackgroundColor:[UIColor clearColor] forState:UIControlStateNormal];
            [self setBackgroundColor:[EPColor epColor_Bg] forState:UIControlStateHighlighted];
        }
            break;
        default:
            break;
    }
    [self stateChange];
}

- (void)setBorderColor:(UIColor*)borderColor forState:(UIControlState)state
{
    [self.borderColorMap setValue:borderColor forKey:[NSString stringWithInteger:state]];
}

- (void)setBackgroundColor:(UIColor *)backgroundColor forState:(UIControlState)state
{
    [self.backgroundColorMap setValue:backgroundColor forKey:[NSString stringWithInteger:state]];
}

#pragma - private
- (NSMutableDictionary*)borderColorMap
{
    if (!_borderColorMap)
    {
        _borderColorMap = [NSMutableDictionary dictionaryWithCapacity:0];
    }
    return _borderColorMap;
}

- (NSMutableDictionary*)backgroundColorMap
{
    if (!_backgroundColorMap)
    {
        _backgroundColorMap = [NSMutableDictionary dictionaryWithCapacity:0];
    }
    return _backgroundColorMap;
}

- (void)addObserver
{
    WEAKSELF
    [RACObserve(self, highlighted) subscribeNext:^(id x) {
        [weakSelf stateChange];
    }];
    [RACObserve(self, selected) subscribeNext:^(id x) {
        [weakSelf stateChange];
    }];
    [RACObserve(self, enabled) subscribeNext:^(id x) {
        [weakSelf stateChange];
    }];
}

- (void)stateChange
{
    NSString *key = [NSString stringWithUInteger:UIControlStateNormal];
    if (self.state == UIControlStateDisabled)
    {
        key = [NSString stringWithUInteger:UIControlStateDisabled];
    }
    else if (self.state & UIControlStateHighlighted)
    {
        key = [NSString stringWithUInteger:UIControlStateHighlighted];
    }
    else if (self.state == UIControlStateSelected)
    {
        key = [NSString stringWithUInteger:UIControlStateSelected];
    }
    
    UIColor *bordorColor = [_borderColorMap valueForKey:key];
    UIColor *backgoundColor = [_backgroundColorMap valueForKey:key];
    
    if (bordorColor)     //不为空时才设，因为系统会有默认的
        self.layer.borderColor = [bordorColor CGColor];
    if (backgoundColor)
        self.backgroundColor = backgoundColor;
}

- (void)buttonWithStyle:(EPBasicButtonStyle)style
{
    if (self) {
        self.epStyle = style;
    }
}

+ (EPButton *)buttonWithStyle:(EPBasicButtonStyle)style
{
    EPButton *button = [EPButton buttonWithType:UIButtonTypeCustom];
    button.epStyle = style;
    return button;
}

+ (EPButton*)buttonWithFrame:(CGRect)rect style:(EPBasicButtonStyle)style
{
    return [[self class] buttonWithFrame:rect style:style target:nil selector:nil];
}

+ (EPButton*)buttonWithFrame:(CGRect)rect style:(EPBasicButtonStyle)style target:(id)target selector:(SEL)selector
{
    EPButton *button = [EPButton buttonWithType:UIButtonTypeCustom];
    button.adjustsImageWhenDisabled = NO;
    button.frame = rect;
    button.epStyle = style;
    [button addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];
    return button;
}

@end

